package cn.mhome.merchant.server.business.service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.mhome.merchant.dao.common.po.Business;
import cn.mhome.merchant.dao.common.po.BusinessVisit;
import cn.mhome.merchant.dao.common.po.ClassBusiness;
import cn.mhome.merchant.dao.jpa.business.BusinessDao;
import cn.mhome.merchant.dao.jpa.business.BusinessVisitDao;
import cn.mhome.merchant.dao.jpa.clazz.ClassBusinessDao;
import cn.mhome.merchant.entity.business.param.BusinessParam;
import cn.mhome.merchant.entity.business.result.BusinessResult;
import cn.mhome.merchant.entity.common.result.CommonResult;
import cn.mhome.merchant.entity.common.result.Pager;
import cn.mhome.merchant.enums.ClassTypeState;
import cn.mhome.merchant.util.DateUtil;

/**
 * 商家管理
 */
@Service
public class BusinessServiceImpl implements BusinessService{

	@Resource
	private BusinessDao businessDao;
	@Resource
	private ClassBusinessDao classBusinessDao;
	@Resource
	private BusinessVisitDao businessVisitDao;
	
	@Override
	@Transactional
	public List<BusinessResult> findForList(BusinessParam businessParam) {
		return businessDao.findForList(businessParam);
	}

	@Override
	@Transactional
	public Pager<BusinessResult> findForPage(BusinessParam businessParam) {
		return businessDao.findForPage(businessParam);
	}

	@Override
	@Transactional
	public CommonResult save(BusinessParam businessParam) {
		CommonResult result = new CommonResult();
		try {
			Long count = 0L;
			if(businessParam.getBusinessId() > 0){
				count = businessDao.findSameCount(businessParam.getBusinessId(),businessParam.getCity(),businessParam.getBusinessName());
			}else{
				count = businessDao.findSameCount(businessParam.getCity(),businessParam.getBusinessName());
			}
			if(count != null && count > 0){
				result.setCode(300);
				result.setMsg("名称重复");
				return result;
			}
			Business business = null;
			if(businessParam.getBusinessId() > 0){
				business = businessDao.findOne(businessParam.getBusinessId());
			}else{
				business = new Business();
				business.setCreateUser(businessParam.getCreateUser());
				business.setCreateTime(DateUtil.getOperateTime());
				business.setState(ClassTypeState.OK.getCode());
			}
			business.setBusinessName(businessParam.getBusinessName());
			business.setCity(businessParam.getCity());
			business.setRemark(businessParam.getRemark());
			business.setTelphone(businessParam.getTelphone());
			business.setState(businessParam.getState());
			business.setBusinessImage(businessParam.getBusinessImage());
			business.setOutletName(businessParam.getOutletName());
			business.setOutletAddress(businessParam.getOutletAddress());
			business.setOutletPhone(businessParam.getOutletPhone());
			business.setOutletLevel(businessParam.getOutletLevel());
			
			business.setOutletDesc(businessParam.getOutletDesc());
			business.setImageUrl1(businessParam.getImageUrl1());
			business.setImageUrl2(businessParam.getImageUrl2());
			business.setImageUrl3(businessParam.getImageUrl3());
			business.setImageUrl4(businessParam.getImageUrl4());
			business.setImageUrl5(businessParam.getImageUrl5());
			business.setOutletDetail(businessParam.getOutletDetail());
			business.setVisitCount(0);
			business.setContactCount(0);
			business = businessDao.save(business);
			result.setCode(200);
			result.setMsg("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		return result;
	}

	@Override
	@Transactional
	public CommonResult delete(BusinessParam businessParam) {
		CommonResult result = new CommonResult();
		Business business = businessDao.findOne(businessParam.getBusinessId());
		/**
		 * 删除关系
		 */
		businessDao.delete(business);
		result.setCode(200);result.setMsg("删除成功");
		return result;
	}
	
	@Override
	@Transactional
	public CommonResult changeState(BusinessParam businessParam) {
		CommonResult result = new CommonResult();
		Business business = businessDao.findOne(businessParam.getBusinessId());
		business.setState(businessParam.getState());
		business = businessDao.save(business);
		result.setCode(200);result.setMsg("设置成功");
		return result;
	}

	
	@Override
	@Transactional
	public CommonResult saveRelation(BusinessParam businessParam) {
		CommonResult result = new CommonResult();
		Business business = businessDao.findOne(businessParam.getBusinessId());
		if(business == null){
			result.setCode(300);
			result.setMsg("商户已被删除");
			return result;
		}
		classBusinessDao.deleteRelationByBId(businessParam.getBusinessId());
		Set<ClassBusiness> cbs = new HashSet<ClassBusiness>();
		for(Long classTypeId : businessParam.getClassTypeIds()){
			ClassBusiness classBusiness = new ClassBusiness();
			classBusiness.setBuinessId(businessParam.getBusinessId());
			classBusiness.setClassId(classTypeId);
			classBusiness.setIndex(10);
			cbs.add(classBusiness);
		}
		classBusinessDao.save(cbs);
		result.setCode(200);result.setMsg("保存成功");
		return result;
	}

	@Override
	@Transactional
	public CommonResult visit(BusinessParam businessParam) {
		CommonResult result = new CommonResult();
		BusinessVisit visit = new BusinessVisit();
		visit.setBusinessId(businessParam.getBusinessId());
		visit.setOpenId(businessParam.getOpenId());
		visit.setVisitTime(new Date());
		visit.setVisitType(businessParam.getVisitType());
		visit = businessVisitDao.save(visit);
		if(businessParam.getVisitType() == 1){
			businessDao.updateVisteCount(businessParam.getBusinessId());
		}else{
			businessDao.updateContactCount(businessParam.getBusinessId());
		}
		return result;
	}
}
